<template>
  <div>
    <div>
      <div style="display: flex;justify-content: space-between">
        <div>
          <el-input placeholder="请输入员工名进行搜索，可以直接回车搜索..."
                    prefix-icon="el-icon-search"
                    clearable
                    @clear="initTrains"
                    style="width: 350px;margin-right: 10px"
                    v-model="keyword"
                    @keydown.enter.native="initTrains"
                    :disabled="showAdvanceSearchView"></el-input>
          <el-button icon="el-icon-search"
                     type="primary"
                     @click="initTrains"
                     :disabled="showAdvanceSearchView">
            搜索
          </el-button>
          <el-button type="primary"
                     @click="showAdvanceSearchView = !showAdvanceSearchView">
            <i :class="showAdvanceSearchView?'fa fa-angle-double-up':'fa fa-angle-double-down'"
               aria-hidden="true"></i>
            高级搜索
          </el-button>
        </div>
        <div>
          <el-upload :show-file-list="false"
                     :before-upload="beforeUpload"
                     :on-success="onSuccess"
                     :on-error="onError"
                     :disabled="importDataDisabled"
                     style="display: inline-flex;margin-right: 8px"
                     action="/system/train/page/import">
            <el-button :disabled="importDataDisabled"
                       type="success"
                       :icon="importDataBtnIcon">
              {{importDataBtnText}}
            </el-button>
          </el-upload>
          <el-button type="success"
                     @click="exportData"
                     icon="el-icon-download">
            导出数据
          </el-button>
          <el-button type="primary"
                     icon="el-icon-plus"
                     @click="showAddTrainView">
            添加用户
          </el-button>
        </div>
      </div>
      <transition name="slide-fade">
        <div v-show="showAdvanceSearchView"
             style="border: 1px solid #409eff;border-radius: 5px;box-sizing: border-box;padding: 5px;margin: 10px 0px;">
          <!--高级查询，暂时用不到-->
        </div>
      </transition>
    </div>
    <div style="margin-top: 10px">
      <el-table :data="trains"
                stripe
                border
                v-loading="loading"
                element-loading-text="正在加载..."
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                style="width: 100%">
        <el-table-column type="selection"
                         width="55">
        </el-table-column>
        <el-table-column prop="name"
                         fixed
                         align="left"
                         label="姓名"
                         width="100">
        </el-table-column>
        <el-table-column prop="trainDate"
                         label="训练日期"
                         align="left"
                         width="300">
        </el-table-column>
        <el-table-column prop="trainContent"
                         label="训练内容"
                         align="center"
                         width="300">
        </el-table-column>
        <el-table-column prop="remark"
                         width="400"
                         align="center"
                         label="备注">
        </el-table-column>

        <el-table-column fixed="right"
                         width="200"
                         label="操作">
          <template slot-scope="scope">
            <el-button @click="showEditTrainView(scope.row)"
                       style="padding: 3px"
                       size="mini">编辑</el-button>
            <el-button style="padding: 3px"
                       size="mini"
                       type="primary">查看高级资料</el-button>
            <el-button @click="deleteTrain(scope.row)"
                       style="padding: 3px"
                       size="mini"
                       type="danger">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex;justify-content: flex-end">
        <el-pagination background
                       @current-change="currentChange"
                       @size-change="sizeChange"
                       layout="sizes, prev, pager, next, jumper, ->, total, slot"
                       :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="title"
               :visible.sync="dialogVisible"
               width="80%">
      <div>
        <el-form :model="train"
                 :rules="rules"
                 ref="trainForm">

          <el-row>
            <el-col :span="7">
              <el-form-item label="选择员工:"
                            prop="eid">
                <el-select v-model="train.eid"
                           placeholder="员工"
                           size="mini"
                           style="width: 200px;">
                  <el-option v-for="item in employees"
                             :key="item.id"
                             :label="item.name"
                             :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="训练日期:"
                            prop="trainDate">
                <el-date-picker v-model="train.trainDate"
                                size="mini"
                                type="date"
                                value-format="yyyy-MM-dd"
                                style="width: 130px;"
                                placeholder="训练日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>

            <el-col :span="7">
              <el-form-item label="训练内容:"
                            prop="trainContent">
                <el-input size="mini"
                          style="width: 200px"
                          prefix-icon="el-icon-edit"
                          v-model="train.trainContent"
                          placeholder="请输入训练内容"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="7">
              <el-form-item label="备注:"
                            prop="remark">
                <el-input size="mini"
                          style="width: 200px"
                          prefix-icon="el-icon-edit"
                          v-model="train.remark"
                          placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="doAddTrain">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 过滤器的数据写在data外面，因为过滤器不能调用this.
const employeeecType = [
  { type: '0', name: '男' },
  { type: '1', name: '女' },
]

export default {
  name: 'PerTrain',
  data() {
    return {
      searchValue: {
        politicId: null,
        nationId: null,
        jobLevelId: null,
        posId: null,
        engageForm: null,
        departmentId: null,
        beginDateScope: null,
      },
      title: '',
      importDataBtnText: '导入数据',
      importDataBtnIcon: 'el-icon-upload2',
      importDataDisabled: false,
      showAdvanceSearchView: false,
      allDeps: [],
      trains: [],
      loading: false,
      popVisible: false,
      popVisible2: false,
      dialogVisible: false,
      total: 0,
      page: 1,
      keyword: '',
      size: 10,
      nations: [],
      joblevels: [],
      employees: [],
      positions: [],
      tiptopDegrees: [
        '本科',
        '大专',
        '硕士',
        '博士',
        '高中',
        '初中',
        '小学',
        '其他',
      ],
      employeeecType: [
        { type: '0', name: '奖励' },
        { type: '1', name: '惩罚' },
      ],

      options: [
        {
          value: '选项1',
          label: '黄金糕',
        },
        {
          value: '选项2',
          label: '双皮奶',
        },
        {
          value: '选项3',
          label: '蚵仔煎',
        },
        {
          value: '选项4',
          label: '龙须面',
        },
        {
          value: '选项5',
          label: '北京烤鸭',
        },
      ],
      inputDepName: '所属部门',
      train: {
        eid: null,
        name: 'cheng',
        trainDate: '',
        trainContent: '',
        remark: '',
      },
      defaultProps: {
        children: 'children',
        label: 'name',
      },
      rules: {
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        gender: [{ required: true, message: '请输入性别', trigger: 'blur' }],
        birthday: [
          { required: true, message: '请输入出生日期', trigger: 'blur' },
        ],
        idCard: [
          { required: true, message: '请输入身份证号码', trigger: 'blur' },
          {
            pattern:
              /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
            message: '身份证号码格式不正确',
            trigger: 'blur',
          },
        ],
        wedlock: [
          { required: true, message: '请输入婚姻状况', trigger: 'blur' },
        ],
        nationId: [{ required: true, message: '请输入您组', trigger: 'blur' }],
        nativePlace: [
          { required: true, message: '请输入籍贯', trigger: 'blur' },
        ],
        politicId: [
          { required: true, message: '请输入政治面貌', trigger: 'blur' },
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '邮箱格式不正确',
            trigger: 'blur',
          },
        ],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入电话号码', trigger: 'blur' }],
        address: [
          { required: true, message: '请输入员工地址', trigger: 'blur' },
        ],
        telephone: [
          { required: true, message: '请输入住宅电话', trigger: 'blur' },
        ],
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    }
  },

  filters: {
    ecTypeFilter(type) {
      const ecTy = employeeType.find((obj) => obj.type === type)
      return ecTy ? ecTy.name : null
    },
  },
  mounted() {
    this.initTrains()
    this.initData()
    this.initPositions()
  },
  methods: {
    searvhViewHandleNodeClick(data) {
      this.inputDepName = data.name
      this.searchValue.departmentId = data.id
      this.popVisible2 = !this.popVisible2
    },
    onError(err, file, fileList) {
      this.importDataBtnText = '导入数据'
      this.importDataBtnIcon = 'el-icon-upload2'
      this.importDataDisabled = false
    },
    onSuccess(response, file, fileList) {
      this.importDataBtnText = '导入数据'
      this.importDataBtnIcon = 'el-icon-upload2'
      this.importDataDisabled = false
      this.initTrains()
    },
    beforeUpload() {
      this.importDataBtnText = '正在导入'
      this.importDataBtnIcon = 'el-icon-loading'
      this.importDataDisabled = true
    },
    exportData() {
      window.open('/system/train/page/export', '_parent')
    },
    emptyTrain() {
      this.train = {
        eid: null,
        name: 'cheng',
        trainDate: '',
        trainContent: '',
        remark: '',
      }
      this.inputDepName = ''
    },
    showEditTrainView(data) {
      this.title = '编辑train信息'
      this.train = data
      this.dialogVisible = true
    },
    deleteTrain(data) {
      this.$confirm(
        '此操作将永久删除【' + data.name + '】, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(() => {
          this.deleteRequest('/employee/train/' + data.id).then((resp) => {
            if (resp) {
              this.initTrains()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    doAddTrain() {
      if (this.train.id) {
        this.$refs['trainForm'].validate((valid) => {
          if (valid) {
            this.putRequest('/employee/train/', this.train).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initTrains()
              }
            })
          }
        })
      } else {
        this.$refs['trainForm'].validate((valid) => {
          if (valid) {
            this.postRequest('/employee/train/add', this.train).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initTrains()
              }
            })
          }
        })
      }
    },
    handleNodeClick(data) {
      this.inputDepName = data.name
      this.train.departmentId = data.id
      this.popVisible = !this.popVisible
    },
    showDepView() {
      this.popVisible = !this.popVisible
    },
    showDepView2() {
      this.popVisible2 = !this.popVisible2
    },
    initPositions() {},
    getMaxWordID() {},
    initData() {
      console.log('执行了initdata函数')
      if (!window.sessionStorage.getItem('employees')) {
        this.getRequest('/employee/ec/getTotalEmployee').then((resp) => {
          if (resp) {
            this.employees = resp.obj
            window.sessionStorage.setItem('employees', JSON.stringify(resp))
          }
        })
      } else {
        this.employees = JSON.parse(window.sessionStorage.getItem('employees'))
      }
      console.log(this.employees)
    },
    sizeChange(currentSize) {
      this.size = currentSize
      this.initTrains()
    },
    currentChange(currentPage) {
      this.page = currentPage
      this.initTrains('advanced')
    },
    showAddTrainView() {
      this.emptyTrain()
      this.title = '添加train'
      //this.getMaxWordID();
      this.dialogVisible = true
    },
    initTrains(type) {
      this.loading = true
      let url = '/employee/train/?page=' + this.page + '&size=' + this.size
      url += '&name=' + this.keyword

      this.getRequest(url).then((resp) => {
        this.loading = false
        if (resp) {
          this.trains = resp.data
          console.log(this.trains)

          this.total = resp.total
        }
      })
    },
  },
}
</script>

<style>
/* 可以设置不同的进入和离开动画 */
/* 设置持续时间和动画函数 */
.slide-fade-enter-active {
  transition: all 0.8s ease;
}

.slide-fade-leave-active {
  transition: all 0.8s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-fade-enter, .slide-fade-leave-to
        /* .slide-fade-leave-active for below version 2.1.8 */ {
  transform: translateX(10px);
  opacity: 0;
}
</style>